/*
 * TextureLoader.cpp
 *
 *  Created on: 03/08/2013
 *      Author: gonzalo
 */

#include "TextureLoader.h"

#include <SOIL/SOIL.h>
#include <iostream>


unsigned int TextureLoader::load_texture(const char * filename) {
	int width, height, channels;
	unsigned char* image_buffer;
	image_buffer = SOIL_load_image(filename, &width, &height, &channels, SOIL_LOAD_RGBA);
	unsigned int id;
	glGenTextures(1, &id);
	glBindTexture(GL_TEXTURE_2D, id);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP); //we repeat the pixels in the edge of the texture, it will hide that 1px wide line at the edge of the cube, which you have seen in the video
	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP); //we do it for vertically and horizontally (previous line)
	//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R, GL_CLAMP); //we do it for vertically and horizontally (previous line)
	//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_R_EXT, GL_CLAMP); //we do it for vertically and horizontally (previous line)

	glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, width, height, 0, GL_RGBA, GL_UNSIGNED_BYTE, image_buffer);
	SOIL_free_image_data(image_buffer); // Probar que funcione
	return id;
}

unsigned int TextureLoader::load_mip_map_texture(const char* filename) {
	unsigned int num;
	int width, height, channels;
	unsigned char* image_buffer;
	glGenTextures(1, &num);

	image_buffer = SOIL_load_image(filename, &width, &height, &channels, SOIL_LOAD_RGBA);
	if (image_buffer == NULL) {
		std::cout << "img was not loaded" << std::endl;
		return -1;
	}

	glBindTexture(GL_TEXTURE_2D, num);

	glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);

	glTexParameteri(GL_TEXTURE_2D, GL_GENERATE_MIPMAP, GL_TRUE);

	gluBuild2DMipmaps(GL_TEXTURE_2D, GL_RGBA, width, height, GL_RGBA, GL_UNSIGNED_BYTE, image_buffer);

	//glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR_MIPMAP_NEAREST);

//      if(generate)
//              glGenerateMipmap(GL_TEXTURE_2D);        //OpenGL 3.0+ way

	SOIL_free_image_data(image_buffer); // Probar que funcione
	return num;
}

unsigned int TextureLoader::load_cube_map_texture(std::string * filenames) {
	//right,left,top,bottom,near,far
	unsigned int tex;
	int width, height, channels;
	unsigned char* image_buffer;
	glGenTextures(1, &tex);
	glBindTexture(GL_TEXTURE_CUBE_MAP, tex);
	unsigned int id;

	for (int i = 0; i < 6; i++) {
		image_buffer = SOIL_load_image(filenames[i].c_str(), &width, &height, &channels, SOIL_LOAD_RGBA);

		glTexImage2D(GL_TEXTURE_CUBE_MAP_POSITIVE_X + i, 0, GL_RGBA, width, height, 0, GL_RGBA,
				GL_UNSIGNED_BYTE, image_buffer);
		glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
		glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
		glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
		glTexParameteri(GL_TEXTURE_CUBE_MAP, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
		SOIL_free_image_data(image_buffer);
	}
	glBindTexture(GL_TEXTURE_CUBE_MAP, 0);
	return tex;
}

